package main.java.com.epam;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;


public class TestDrive {

	public static void main(String[] args) {

		RingBuffer ringBuffer1 = new RingBuffer(10);
		RingBuffer ringBuffer2 = new RingBuffer(100);
		
		ExecutorService exec = Executors.newCachedThreadPool(new DaemonThreadFactory()); 
		for(int i = 0; i < 5; i++) 
			exec.execute(new Adder(ringBuffer1));
		exec.shutdown();
		
		ExecutorService exec2 = Executors.newCachedThreadPool(new DaemonThreadFactory()); 
		for(int i = 0; i < 3; i++) 
			exec2.execute(new Transferer(ringBuffer1, ringBuffer2));
		exec2.shutdown();
		
	try {
		Thread.currentThread().sleep(2000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	int a = 0;
	for(String e:ringBuffer2.getData()){
		a++;
		System.out.println(a + " = " + e);	
	}
	
	
	}
}

class DaemonThreadFactory implements ThreadFactory { 
	public Thread newThread(Runnable r) { 
		Thread t = new Thread(r);
		t.setDaemon(true); 
		return t;
	}
} 
